package com.mobilepay.moneypig.model.activity.mine;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.mobilepay.moneypig.R;
import com.mobilepay.moneypig.base.BaseActivity;
import com.mobilepay.moneypig.config.Key;
import com.mobilepay.moneypig.config.NetConfig;
import com.mobilepay.moneypig.model.application.App;
import com.mobilepay.moneypig.model.dialog.ConfirmDialog;
import com.mobilepay.moneypig.model.dialog.PasswordDialog;
import com.mobilepay.moneypig.model.dialog.TipDialog;
import com.mobilepay.moneypig.okhttp.HttpUtils;
import com.mobilepay.moneypig.response.ResponseResult;
import com.mobilepay.moneypig.response.mine.UserBlance;
import com.mobilepay.moneypig.utils.CommonUtil;
import com.mobilepay.moneypig.utils.GsonUtil;
import com.mobilepay.moneypig.utils.PreferenceUtil;

/**
 * Created by xlm on 2017/7/24 0024
 */

public class InComeActivity extends BaseActivity {
    private static final String TAG = "InComeActivity";
    private TextView mTextMoney;
    private RelativeLayout mRelativeBack;
    private RelativeLayout mRelativeDetail;
    private RelativeLayout mTextTixian;
    private TextView mTextDescribe;
    private String mTixianfei;

    @Override
    protected void onSetContentView() {
        setContentView(R.layout.activity_income);
    }

    @Override
    protected void initViews() {
        mRelativeDetail = (RelativeLayout) findViewById(R.id.income_detail);
        mRelativeBack = (RelativeLayout) findViewById(R.id.income_back);
        mTextTixian = (RelativeLayout) findViewById(R.id.income_tixian);
        mTextMoney = (TextView) findViewById(R.id.income_num);
        mTextDescribe = (TextView) findViewById(R.id.income_describe);
    }

    @Override
    protected void afterOnCreate(Bundle savedInstanceState) {
        getMoney();
    }

    /**
     * 提现成功后返回
     *
     * @param intent
     */
    @Override
    protected void onNewIntent(Intent intent) {
        getMoney();
    }

    private PasswordDialog passwordDialog;

    private void showPasswordDialog() {
        if (passwordDialog == null) {
            passwordDialog = new PasswordDialog(this);
        }
        passwordDialog.setCancelable(true);
        passwordDialog.show();
        passwordDialog.setListener(new PasswordDialog.Listener() {
            @Override
            public void onConfirm(String ps) {
                if (CommonUtil.isEmpty(ps)) {
                    showToastShort("请输入登陆密码");
                } else {
                    String password = (String) PreferenceUtil.getParam(getThis(), Key.PASSWORD, "");
                    if (!CommonUtil.isEmpty(password) && !password.equals(ps)) {
                        showToastShort("密码错误");
                    } else {
                    }
                }
            }
        });
    }


    private TipDialog confirmCardDialog;

    private void showCardConfirm(String txt) {
        if (confirmCardDialog == null) {
            confirmCardDialog = new TipDialog(this);
        }
        confirmCardDialog.setContent(txt);
        confirmCardDialog.setCancelable(true);
        confirmCardDialog.setConfirm("去绑定");
        confirmCardDialog.show();
        confirmCardDialog.setListener(new TipDialog.Listener() {
            @Override
            public void onConfirm() {
                super.onConfirm();
                confirmCardDialog.dismiss();
                startActivity(AddCardActivity.class, false, false);
            }

            @Override
            public void onDismiss() {
                super.onDismiss();
                confirmCardDialog.dismiss();
            }
        });
    }

    private TipDialog mTipdialog;

    private void showTipDialog() {
        if (mTipdialog == null) {
            mTipdialog = new TipDialog(this);
        }
        mTipdialog.setCancelable(false);
        mTipdialog.setContent("请先进行实名认证");
        mTipdialog.setConfirm("去认证");
        mTipdialog.show();
        mTipdialog.setListener(new TipDialog.Listener() {
            @Override
            public void onConfirm() {
                super.onConfirm();
                mTipdialog.cancel();
                startActivity(IdentifyActivity.class, false, false);//实名认证
            }

            @Override
            public void onDismiss() {
                super.onDismiss();
                mTipdialog.cancel();
            }
        });
    }

    @Override
    protected void onSetListener() {
        mRelativeDetail.setOnClickListener(this);
        mRelativeBack.setOnClickListener(this);
        mTextTixian.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int authStatus = App.getApp().getUserDetailInfo().getData().getReal_auth_status();
                int bankStaus = App.getApp().getUserDetailInfo().getData().getBank_auth_status();
                switch (authStatus) {
                    case 0:
                        showTipDialog();
                        break;
                    case 1:
                        showToastShort("认证审核中");
                        break;
                    case 2:
                        if (0 == bankStaus) {
                            showCardConfirm("请先绑定结算卡");
                        } else {
                            Intent intent = new Intent();
                            intent.putExtra("money", money);
                            intent.putExtra("tixianfei", mTixianfei);
                            intent.setClass(getThis(), TixianActivity.class);
                            startActivity(intent);
                        }
                        break;
                }
            }
        });
//        mTextTixian.setOnClickListener(new NoDoubleClickListener() {
//            @Override
//            public void onNoDoubleClick(View view) {
//                super.onNoDoubleClick(view);
////                int authStatus = App.getApp().getUserInfo().getData().getReal_auth_status();
//                int bankStaus = App.getApp().getUserInfo().getData().getBank_auth_status();
//                if (0 == bankStaus) {
//                    showConfirmDialog("请先绑定提现账户银行卡");
////                            showCardDialog();//添加银行卡
//                } else {
//                    Intent intent = new Intent();
//                    intent.putExtra("money", money);
//                    intent.putExtra("tixianfei", mTixianfei);
//                    intent.setClass(getThis(), TixianActivity.class);
//                    startActivity(intent);
//                }
//                switch (authStatus) {
//                    case 0:
////                        showTipDialog();
////                        break;
//                    case 1:
////                        showConfirmDialog("认证信息审核中，请耐心等待");
//                        showConfirmDialog("请先通过实名认证");
//                        break;
//                    case 2:
//                        if (0 == bankStaus) {
//                            showConfirmDialog("请先绑定银行卡");
////                            showCardDialog();//添加银行卡
//                        } else {
//                            Intent intent = new Intent();
//                            intent.putExtra("money", money);
//                            intent.putExtra("tixianfei", mTixianfei);
//                            intent.setClass(getThis(), TixianActivity.class);
//                            startActivity(intent);
//                        }
//                        break;
//                }
//            }
//        });
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {
            case R.id.income_detail:
                startActivity(IncomeListActivity.class, false, false);
                break;
            case R.id.income_back:
                finish();
                break;
        }
    }

//    private TipDialog mTipdialog;
//
//    private void showTipDialog() {
//        if (mTipdialog == null) {
//            mTipdialog = new TipDialog(getThis());
//        }
//        mTipdialog.setCancelable(false);
//        mTipdialog.setContent("请先进行实名认证");
//        mTipdialog.show();
//        mTipdialog.setListener(new TipDialog.Listener() {
//            @Override
//            public void onConfirm() {
//                super.onConfirm();
//                mTipdialog.cancel();
//                startActivity(IdentifyActivity.class);//实名认证
//            }
//
//            @Override
//            public void onDismiss() {
//                super.onDismiss();
//                mTipdialog.cancel();
//            }
//        });
//    }

    private ConfirmDialog mConfirmDialog;

    private void showConfirmDialog(String content) {
        if (mConfirmDialog == null) {
            mConfirmDialog = new ConfirmDialog(getThis());
        }
        mConfirmDialog.setCancelable(true);
        mConfirmDialog.setContent(content);
        mConfirmDialog.show();
        mConfirmDialog.setListener(new ConfirmDialog.Listener() {
            @Override
            public void onConfirm() {
                super.onConfirm();
                mConfirmDialog.cancel();
            }
        });
    }

    private TipDialog mCarddialog;

    private void showCardDialog() {
        if (mCarddialog == null) {
            mCarddialog = new TipDialog(this);
        }
        mCarddialog.setCancelable(false);
        mCarddialog.setContent("您尚未绑定银行卡,是否绑定？");
        mCarddialog.show();
        mCarddialog.setListener(new TipDialog.Listener() {
            @Override
            public void onConfirm() {
                super.onConfirm();
                startActivity(AddCardActivity.class, false, false);
                mCarddialog.dismiss();
            }

            @Override
            public void onDismiss() {
                super.onDismiss();
                mCarddialog.dismiss();
            }
        });
    }

    String money;

    private void getMoney() {
        HttpUtils.RequestParam param = new HttpUtils.RequestParam();
        param.add("method", NetConfig.get_money);
        param.add("uid", String.valueOf(App.getApp().getUserInfo().getData().getUid()));
        param.add("sign", param.getMd5String());
        HttpUtils.getInstance().params(param).executePost(new HttpUtils.HttpListener() {
            @Override
            public void onException(Throwable e) {
            }

            @Override
            public void onHttpSuccess(String string) {
                super.onHttpSuccess(string);
                if (HttpUtils.isJson(string)) {
                    ResponseResult responseResult = GsonUtil.GsonToBean(string, ResponseResult.class);
                    if (responseResult.getStatus() == 0) {
                        showToastShort(responseResult.getInfo());
                    } else {
                        final UserBlance blance = GsonUtil.GsonToBean(string, UserBlance.class);
                        money = blance.getData().getYes_money();
                        mTixianfei = blance.getData().getDescribe();
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                mTextMoney.setText(money);
                                mTextDescribe.setText(blance.getData().getDescribe());
                            }
                        });
                    }
                }
            }
        });
    }
}
